package com.lkei.controller;

import com.lkei.dto.Result;
import com.lkei.entity.Comment;
import com.lkei.entity.Topic;
import com.lkei.service.CommentService;
import com.lkei.service.TopicService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

//@RestController
//@RequestMapping("topic")
@Controller
public class CommentController {
    @Autowired
    private TopicService topicService;

    @Autowired
    private CommentService commentService;

    @GetMapping("/topic_detail/{tno}")
    public String TopicDetail(@PathVariable("tno") String tno, Model model, HttpSession session, Topic topic){
        Topic thistopic = topicService.findTopicById(tno);
        model.addAttribute("thistopic",thistopic);
        //String userNo = (String) session.getAttribute("userNo");
        List<Comment> comments = commentService.findCommentByTno(tno);
        model.addAttribute("comments",comments);
//        ra.addAttribute("pn",pn);
        return "topic_detail";
    }
    //    @GetMapping("topic_detail")
//    public String topicdetailPage(){
//        return "topic_detail";
//    }
    @PostMapping(value = {"/addcomment/{tno}"})
    public String addTopicPage(@PathVariable("tno") String tno, Comment comment,HttpSession session, RedirectAttributes ra){
        //        Timestamp t = new Timestamp(new Date().getTime());
        //        SimpleDateFormat sdf = new SimpleDateFormat("yyy");
        //        topic.setPosttime(t);
        //        topicMapper.insert(topic);
        DateTime time = new DateTime();
        String ti = time.toString("yyyy-MM-dd HH:mm:ss");
        String ti2 = time.toString("yyyyMMddHHmmss");
        comment.setCtime(ti);
        comment.setTno(tno);
        ti2 = ti2+"124001";
        comment.setCno(ti2);
        comment.setCname("124001");
//        comment.setCcontent(comment.getCcontent());
        commentService.addComment(comment);
        return ("redirect:/topic_detail/"+tno);
    }
//    @PostMapping(value = {"/addcomment"})
//    public void addTopicPage(String tno, String cno, String ccontent){
//        Comment comment = new Comment();
//        comment.setTno(tno);
//        comment.setCcontent(ccontent);
//        comment.setCno(cno);
//        commentMapper.insert(comment);
//
//    }


}